package club.xiaojiawei.dp;

/**
 * @author 肖嘉威
 * @version 1.0
 * @date 6/22/22 10:40 PM
 * @question 5. 最长回文子串
 * @description 给你一个字符串 s，找到 s 中最长的回文子串。
 */
public class LongestPalindrome5 {

    public static void main(String[] args) {
        LongestPalindrome5 test = new LongestPalindrome5();
        String result = test.longestPalindrome4("babadada");
        System.out.println("最长回文子串："+result);
    }

    /**
     * 马拉车算法
     * 时间复杂度：O(n)
     * 时间复杂度：O(n)
     * @param s
     * @return
     */
    public String longestPalindrome(String s) {
        char[] chars = new char[(s.length() << 1) + 1];
        int index = 0;
        for (int i = 0; i < s.length(); i++){
            chars[index++] = '#';
            chars[index++] = s.charAt(i);
        }
        chars[chars.length - 1] = '#';
        int[] radius = new int[chars.length];
        int r = -1;
        int c = -1;
        int max = Integer.MIN_VALUE;
        int center = 0;
        for (int i = 0; i < chars.length; i++){
            radius[i] = r > i? Math.min(radius[2 * c - i], r - i + 1) : 1;
            while (i + radius[i] < chars.length && i - radius[i] > -1){
                if (chars[i - radius[i]] == chars[i + radius[i]]){
                    radius[i]++;
                }else {
                    break;
                }
            }
            if (i + radius[i] > r){
                r = i + radius[i];
                c = i;
            }
            if (radius[i] > max){
                max = radius[i] - 1;
                center = i;
            }
        }
        StringBuilder result = new StringBuilder();
        for (int i = center - max + 1; i < center + max; i++){
            if (chars[i] != '#'){
                result.append(chars[i]);
            }
        }
        return result.toString();
    }

    /**
     * 官方-dp
     * 时间复杂度：O(n^2)
     * 时间复杂度：O(n^2)
     * @param s
     * @return
     */
    public String longestPalindrome2(String s) {
        int len = s.length();
        if (len < 2) {
            return s;
        }
        int maxLen = 1;
        int begin = 0;
        // dp[i][j] 表示 s[i..j] 是否是回文串
        boolean[][] dp = new boolean[len][len];
        // 初始化：所有长度为 1 的子串都是回文串
        for (int i = 0; i < len; i++) {
            dp[i][i] = true;
        }
        char[] charArray = s.toCharArray();
        // 递推开始
        // 先枚举子串长度
        for (int L = 2; L <= len; L++) {
            // 枚举左边界，左边界的上限设置可以宽松一些
            for (int i = 0; i < len; i++) {
                // 由 L 和 i 可以确定右边界，即 j - i + 1 = L 得
                int j = L + i - 1;
                // 如果右边界越界，就可以退出当前循环
                if (j >= len) {
                    break;
                }
                if (charArray[i] != charArray[j]) {
                    dp[i][j] = false;
                } else {
                    if (j - i < 3) {
                        dp[i][j] = true;
                    } else {
                        dp[i][j] = dp[i + 1][j - 1];
                    }
                }
                // 只要 dp[i][L] == true 成立，就表示子串 s[i..L] 是回文，此时记录回文长度和起始位置
                if (dp[i][j] && j - i + 1 > maxLen) {
                    maxLen = j - i + 1;
                    begin = i;
                }
            }
        }
        return s.substring(begin, begin + maxLen);
    }

    /**
     * 官方-中心扩展
     * 时间复杂度：O(n^2)
     * 时间复杂度：O(1)
     * @param s
     * @return
     */
    public String longestPalindrome3(String s) {
        if (s == null || s.length() < 1) {
            return "";
        }
        int start = 0, end = 0;
        for (int i = 0; i < s.length(); i++) {
            int len1 = expandAroundCenter(s, i, i);
            int len2 = expandAroundCenter(s, i, i + 1);
            int len = Math.max(len1, len2);
            if (len > end - start) {
                start = i - (len - 1) / 2;
                end = i + len / 2;
            }
        }
        return s.substring(start, end + 1);
    }

    public int expandAroundCenter(String s, int left, int right) {
        while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
            --left;
            ++right;
        }
        return right - left - 1;
    }


    /**
     * 民间-中心扩展
     * @param s
     * @return
     */
    public String longestPalindrome4(String s) {
        start = 0;
        end = 0;
        cs = s.toCharArray();
        len = cs.length;
        process(0);
        return s.substring(start, end + 1);
    }

    int len;
    char[] cs;
    int start, end;

    public void process(int i){
        if(i >= len || 2 * (len - i) < end - start) {
            return;
        }
        int startN = i, endN = i;
        while(endN + 1 < len && cs[endN] == cs[endN + 1]) {
            endN++;
        }
        i = endN;
        while(startN > 0 && endN + 1 < len && cs[startN - 1] == cs[endN + 1]){
            startN--;
            endN++;
        }
        if(endN - startN > end - start){
            end = endN;
            start = startN;
        }
        process(i + 1);
    }
}
